<template>
  <div class="event-detail" v-if="event">
    <el-row :gutter="20">
      <el-col :span="12">
        <div class="event-image-container">
          <img :src="event.imageUrl" :alt="event.title" class="event-image" />
        </div>
      </el-col>
      <el-col :span="12">
        <h1>{{ event.title }}</h1>
        <el-descriptions :column="1" border>
          <el-descriptions-item label="演出时间">{{ event.date }}</el-descriptions-item>
          <el-descriptions-item label="演出场馆">{{ event.venue }}</el-descriptions-item>
          <el-descriptions-item label="演出城市">{{ event.city }}</el-descriptions-item>
          <el-descriptions-item label="票价">
            <el-tag type="danger" size="large">¥{{ event.price }}</el-tag>
          </el-descriptions-item>
        </el-descriptions>
        
        <div style="margin-top: 20px;">
          <el-button type="primary" size="large" @click="buyTicket">立即购票</el-button>
          <el-button size="large" @click="addToCart">加入购物车</el-button>
        </div>
      </el-col>
    </el-row>

    <el-tabs v-model="activeTab" style="margin-top: 30px;">
      <el-tab-pane label="演出详情" name="detail">
        <p>{{ event.description }}</p>
      </el-tab-pane>
      <el-tab-pane label="购票须知" name="notice">
        <p>1. 请提前30分钟入场</p>
        <p>2. 一人一票，凭票入场</p>
        <p>3. 售出票品不支持退换</p>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAppStore } from '../store'
import { ElMessage } from 'element-plus'
import api from '../api'

export default {
  name: 'EventDetail',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const store = useAppStore()
    
    const event = ref(null)
    const activeTab = ref('detail')

    // 从API获取事件详情
    const fetchEventDetail = async () => {
      try {
        const eventId = route.params.id
        const response = await api.get(`/events/${eventId}`)
        
        // 检查响应是否为有效的事件对象
        if (response && typeof response === 'object' && response.id) {
          event.value = response
        } else {
          throw new Error('Invalid event data received')
        }
      } catch (error) {
        console.error('Failed to fetch event detail:', error)
        ElMessage.error('获取演出详情失败')
        // 如果获取失败，可以跳转到错误页面或返回首页
        setTimeout(() => {
          router.push('/')
        }, 2000)
      }
    }

    onMounted(() => {
      fetchEventDetail()
    })

    const buyTicket = () => {
      if (!event.value) return
      
      if (!localStorage.getItem('token')) {
        ElMessage.warning('请先登录')
        router.push('/login')
        return
      }
      store.addToCart(event.value)
      router.push('/orders')
    }

    const addToCart = () => {
      if (!event.value) return
      
      if (!localStorage.getItem('token')) {
        ElMessage.warning('请先登录')
        router.push('/login')
        return
      }
      store.addToCart(event.value)
      ElMessage.success('已添加到购物车')
    }

    return {
      event,
      activeTab,
      buyTicket,
      addToCart
    }
  }
}
</script>

<style scoped>
.event-image-container {
  height: 400px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.event-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.event-image:hover {
  transform: scale(1.03);
}
</style>